function QT = ObjFctGMMuni1(x);
global P ERROR_SERIES INSTRUMENTS N_ITER WEIGHT_WT_1 PAR_N_ITER_1;

% OBJ_FCT_GMMUNI_1.M           Version I.K. - 22/12/06
%   Constructs the objective function involved in the GMM estimation 
%   for a naive loss parameters: 
%       L1(p,alpha_1,e_1) + ... + L1(p,alpha_d,e_d) = (alpha - t.*(e < 0))'*(abs(e).^p)
% where
%   forecast error e = (e_1,...,e_d)'
%   exponent p is scalar
%   asymmetry vector t = (2*alpha_1-1,...,2*alpha_d-1)'
%  
% NOTATION (Hamilton, Ch14) & COMMENTS:
% 	x = [p, alpha_1, ..., alpha_N]'            
% 	Vt = K-VECTOR OF INSTRUMENTS with K >= N+1
% 	h(x,Vt) = []'
% 	g(x) = mean(h(x,Vt))		( eq 14.1.10 )
% 	Q(x) = g(x)'*WT*g(x)		( eq 14.1.11 )
% 	where WT = Id(rxr) at the first iteration (cf. Hamilton pg413)
% The series used in the objective function is the global variable ARMA_SERIES.
% This objective function is called several times and the weighting matrix WEIGHT_WT
% (global) changes at each iteration. The number of iterations is stored in the global
% variable N_ITER and the estimator obtained at iteration N_ITER is stored in
% the global variable PAR_N_ITER.

T = size(ERROR_SERIES,1);
N = size(ERROR_SERIES,2);
p = P;
alpha = x(1:(N));
tau = 2*alpha - 1;
% Computing the gradient T x N matrix of the maive loss L(p,alpha,e) wrt e
grad_p = [];
for i=1:1:N
    grad_p = [grad_p, (tau(i) + sign(ERROR_SERIES(:,i))).*(abs(ERROR_SERIES(:,i)).^(p-1))];
end

% Computing the vector function h(x,Vt) 
h = [];
for i=1:1:T
    h = [h; kron(grad_p(i,:),INSTRUMENTS(i,:))];   
end
% computing the function g(x)
g = sum(h)'/size(h,1);	
% computing the weighting matrix WT 
if N_ITER == 1
   WT_1 = eye(size(g,1),size(g,1));
else
   WT_1 = WEIGHT_WT_1; 
end
% computing the objective function to minimize QT(x) 
QT = g'*WT_1*g;               % eq. 14.1.11 
